package com.hddf.project.auth.actions;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.kommii.log.LogNames;

import com.hddf.project.auth.service.CookieService;
import com.hddf.project.auth.service.MongoDB_Query;
import com.hddf.project.auth.service.WebLoginService;



import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;


public class LogoutAction extends BaseAction{
	
	private static final long serialVersionUID = 15568767546435L;
	
	public String weblogout(){
		Map<String,Object> map = new HashMap<String,Object>();
		HttpServletRequest request = getRequest();
		HttpServletResponse response = getResponse();
		try {
			CookieService cs = CookieService.getInstance();
			MongoDB_Query mq = MongoDB_Query.getInstance();
			WebLoginService wls = WebLoginService.getInstance();
			Cookie ctoken = cs.getCookieByName(request, "to");
			Cookie ctime = cs.getCookieByName(request, "ti");
			Cookie cy = cs.getCookieByName(request, "y");
			Cookie ck= cs.getCookieByName(request, "k");
			if(null!=ctoken){
				String web_token = ctoken.getValue();
				DBObject temp =  mq.getSimpleUserByWebToken(web_token);
				if(null!=temp){
					int uid = (Integer)temp.get("user_id");
					BasicDBObject obj = new BasicDBObject();
					obj.put("user_id", uid);
					obj.put("aes_key", "");
					obj.put("webtoken_lifetime", 0);
					obj.put("web_token", "");
					wls.logout(obj);
					request.setAttribute("logname",LogNames.login);
					request.setAttribute("logs","logout state ok"+
							" , user name:"+temp.get("username")+" ,user id: "+temp.get("user_id"));
				}
				cs.addCookie(response, "to", null, 0);
			}
			if(null!=ctime){
				cs.addCookie(response, "ti", null, 0);
			}
			if(null!=cy){
				cs.addCookie(response, "y", null, 0);
			}
			if(null!=ck){
				cs.addCookie(response, "k", null, 0);
			}
			
			return "login";
		}catch(Exception e){
			e.printStackTrace();
			//map.put("state", "error");
			//request.setAttribute("result", JsonProvider.getCommonJson(map));
			return "login";
		}
	}

}
